package com.jy.rhin.support.interceptor.convertor;

import cn.org.chiss.rhin._2015.registration.terminology.ValueSetQueryRequest;
import cn.org.chiss.rhin._2015.registration.terminology.ValueSetQueryResponse;
import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @Description: 术语注册服务-查询值集接口，打包审计信息
 * @Author: lww
 * @Date: 2022/5/23
 */
@Service("AuditInfoConverter4TerminologyValueSetQuery")
public class AuditInfoConverter4TerminologyValueSetQuery extends BaseAuditInfoConverter<ValueSetQueryRequest, ValueSetQueryResponse> {

    public AuditInfoConverter4TerminologyValueSetQuery() {
        super(EventActionCodeEnum.READ,
                ServiceCodeEnum.TERMINOLOGY_REGISTRATION,
                EventTypeCodeEnum.VALUE_SET_QUERY,
                true,
                ParticipantObjectIDTypeCodeEnum.SEARCH_CRITERIA,
                ParticipantObjectTypeCodeRoleEnum.RESOURCE,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ACCESS_USE);
    }

    /**
     * 审计源（审计源为事件目标）
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO: 2022/5/24 必传 审计源的标识
        auditSource.setBiaoShi("terminology-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.TERMINOLOGY_REGISTRATION.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(ValueSetQueryRequest valueSetQueryRequest, ValueSetQueryResponse valueSetQueryResponse) {
        return new AuditParticipantUserDesc("client-1", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(ValueSetQueryRequest valueSetQueryRequest, ValueSetQueryResponse valueSetQueryResponse) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(ValueSetQueryRequest valueSetQueryRequest, ValueSetQueryResponse valueSetQueryResponse) {
        return new AuditParticipantUserDesc("terminology-service", ServiceCodeEnum.TERMINOLOGY_REGISTRATION.getName());
    }

    @Override
    String getObjectId(ValueSetQueryRequest valueSetQueryRequest, ValueSetQueryResponse valueSetQueryResponse) {
        if (Objects.nonNull(valueSetQueryRequest)) {
            return "";
        }
        if (Objects.nonNull(valueSetQueryResponse)
                && Objects.nonNull(valueSetQueryResponse.getValueSet())
                && !valueSetQueryResponse.getValueSet().isEmpty()) {
            return valueSetQueryResponse.getValueSet().stream().map(valueSet -> valueSet.getId().getValue()).collect(Collectors.joining(","));
        }
        return "";
    }
    
    @Override
   	public boolean isMatch(String method, Object data) {
    	return "ValueSetProviderPortImpl.valueSetQuery".equals(method);
   	}

}
